home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-07-15 | 10.4 KB | 382 lines | [TEXT/MPS ] |
- {TaskCtl.p
- 2/13/92 3:15:46 PM gs added dynamic check for sleep values.
- 13.2.1988 22:22:15 gs }
-
- unit TaskCtl;
- {© G. Sawitzki, StatLab Heidelberg 1986-1992}
-
- {You only have to change this file if you want to add further menus,
- or if you want to implement a non Generic event handling.
-
- You will rarely want to program a nongeneric response to an event. But if you
- want to, the recommended procedure is: Make a copy of the Generic event handler
- which you find in the unit "Generic". Paste the copy to your "TaskCtl". Rename
- it to something like My... throughout TaskCtl and adopt it to your needs. An
- example for this is given in "Random".
- Usually, you will just enhance the Generic Action, so My... will look like
- begin
- Appl...; - the Generic Action -
- xxxxxxx; - your enhancement
- end;
- }
-
- interface
-
- uses
- MacUnits, { Standard Includes}
- StdTools, Generic,
- NetWork,NetWorkLookUp,
- ObjIntf,SchedulerUnit,NetSimGlobal, Task;
-
- {tuning constants}
- const
- cForeGroundSleep = 0;{ticks to wait for wake up}
- cBackgroundSleep=20;
-
- {----------------------------command stuff----------------------}
- function mygrowzone (cbneeded: size): size;
- procedure MainEventLoop;
- procedure HandleFinderRequest;
-
- implementation
- {-------------------- Generic Part -------------------------}
- procedure HandleFinderRequest;
- begin
- end;
-
- {----------------Emergency exits this way-----------------}
-
-
- function mygrowzone;
- begin
- setfontlock(false);
- mygrowzone := 0;
- end;
-
-
- {-------------------- Application Part -------------------------}
-
- {-------------------- Generic Part/ Menu Stuff-------------------------}
- procedure GenericFileMenu (theitem: integer);
-
- begin
- {the last item should be quit}
- if theitem = countMItems(GetMHandle(mFile)) then
- gTaskState := TaskExit
- else
- case theitem of
- 1:
- doopen(cnew); {New}
- 2:
- doopen(cOpen); {Open}
- 3:
- doclose(cClose); {Close}
- 4:
- dosave(cSave); {Save}
- 5:
- dosave(cSaveAs); {Save as...}
- 6:
- ;{(-}
- 7:
- ;{Print...}
- 8:
- ;{Page Setup...}
- end;{case}
- {Last entry should be Quit}
- end;
-
- {----------------------------command stuff-----------------------------------}
- procedure DoCommand (thecommand: Longint);
- var
- theMenu, theitem: integer;
-
-
-
- begin
- theMenu := hiword(thecommand);
- theitem := loword(thecommand);
- case theMenu of
- mApple:
- GenericApple(theitem); {will be handled generically}
- mFile:
- GenericFileMenu(theitem); { The File menu. }
- mEdit:
- if not systemedit(theitem - 1) then
- GenericEdit(theitem); { The Edit menu. }
- { Check for accessory edit; if not, call the application edit. }
- otherwise
- end;{case}
- hiliteMenu(0); { Turn off the menu hiliting. }
- end;
-
- procedure DoMouseDown (event: EventRecord);
- var
- TempFrontWindow, WhichWindow: WindowPtr; { Which window from findwindow. }
- WhichWindowRef: longint;
- fwreturncode: integer; { findwindow return code. }
- safeport: grafptr; {Pointer to current port will be saved here during updates }
- key: char; { Keyboard character. }
- keycommand: Longint; { Keybd input translated to menu equivs. }
-
- begin
- with event do
- begin
-
- { Get the window/window part. This is a common information. So we extract it once.}
- fwreturncode := findwindow(where, WhichWindow);
-
- case fwreturncode of
-
- insyswindow:
- systemclick(Event, WhichWindow); { If in a system window, let the system Handle it. }
-
-
- inMenubar:
- docommand(Menuselect(where)); { If in the menu bar, track it, then docommand }
-
- indrag:
- GenericDrag(Event, WhichWindow);
-
- ingrow, incontent:{ If in the interior... }
- begin { If this window isn't in front, make it be so. }
- if (WhichWindow <> frontwindow) then
- selectwindow(WhichWindow)
- { Otherwise, let the application Handle it. }
- else
- begin
- getport(safeport);
- setport(WhichWindow);
- if fwreturncode = incontent then
- GenericMousedown(Event, WhichWindow)
- else
- GenericGrowwindow(Event, WhichWindow);
- setport(safeport);
- end;
- end;
-
- { If in the go away box, track and call the appl}
- ingoaway:
- if trackgoaway(WhichWindow, where) then
- begin
- GenericGoaway(WhichWindow);
- if WhichWindow <> nil then
- begin
- getport(safeport);
- DisposeWindow(WhichWindow);
- if safeport = WhichWindow then {avoid dangling grafport}
- begin
- TempFrontWindow := frontwindow;
- if TempFrontWindow = nil then
- begin
- GetWMgrPort(TempFrontWindow);{26.11.1990 0:53:50 Uhr gs }
- setport(TempFrontWindow);
- end
- else
- setwport(TempFrontWindow);
- end;{avoid dangling grafport}
- end;{whichwindow<>nil}
- end;{goAway}
-
- InZoomIn, InZoomout:
- if trackBox(WhichWindow, where, fwreturncode) then
- begin
- getport(safeport);
- setwport(whichwindow);
- GenericZoomWindow(fwreturncode,Event, WhichWindow);
- setport(safeport);
- end;{Zoom}
- otherwise
- end; {fwreturncode}
- end; {mousedown}
- end;
-
- {******************** The Event handler *****************}
- {DoEvent has been moved out of the main event loop to allow immediate testing}
-
- procedure DoEvent (event: EventRecord);
- var
- TempFrontWindow, WhichWindow: WindowPtr; { Which window from findwindow. }
- WhichWindowRef: longint;
- fwreturncode: integer; { findwindow return code. }
- safeport: grafptr; {Pointer to current port will be saved here during updates }
- key: char; { Keyboard character. }
- keycommand: Longint; { Keybd input translated to menu equivs. }
-
- begin
- with Event do
- begin
- case what of
- mousedown:
- DoMouseDown(event);
-
- { Keyboard events: }
- keydown, autokey:
- begin
- key := chr(bitand(Message, 255));
- if bitand(modifiers, cmdkey) <> 0 then
- keycommand := Menukey(key);
- if (what = keydown) and (bitand(modifiers, cmdkey) <> 0) and (hiword(keycommand) <> 0) then
- docommand(keycommand)
- else
- GenericKeydown(Event);
- end;
-
- { Activate/deactivate window: }
- ActivateEvt:
- begin
- getport(safeport);
- setport(WindowPtr(Message));
- if bitand(modifiers, 1) <> 0 then
- GenericActivate(WindowPtr(Message))
- else
- GenericDeactivate(WindowPtr(Message));
- setport(safeport);
- end;
-
-
- updateEvt: { Update window: }
- begin
- getport(safeport);
- setport(WindowPtr(Message));
- beginupdate(WindowPtr(Message));
- if isDialogEvent(Event) then
- drawdialog(Dialogptr(Message))
- else
- begin
- drawcontrols(WindowPtr(Message));
- GenericUpdate(WindowPtr(Message));
- end;
- endupdate(WindowPtr(Message));
- setport(safeport);
- end;
-
- diskEvt:
- GenericDiskEvent(Message);{ Disk insertion event: }
-
- {*******************}
- NetWorkEvt: begin
- NetWorkScheduler.HandleMsg(MsgPtr(message)); {required}
- NetWorkScheduler.periodictask; {optional: we want to give a prime priority to the scheduler}
- end;
- {*******************}
-
- app4Evt:
- begin
- TempFrontWindow := frontWindow;{26.11.1990 1:01:39 Uhr gs }
- if bitand(Message, $FF000000) = $01000000 then {suspend or resume}
- begin
- if odd(Message) then
- begin {will go to front}
- gWaitNextEventSleep:=cForegroundSleep;
- MyTask(BackEnd);
- gInBackground := false;
- if TempFrontWIndow <> nil then begin
- selectwindow(TempFrontWindow); {should not be necessary, but is...}
- invalrect(thePort^.PortRect);
- end;
- end
- else
- begin {will go to background}
- gWaitNextEventSleep:=cBackgroundSleep;
- MyTask(BackBegin);
- gInBackground := true;
- if TempFrontWIndow <> nil then
- GenericDeactivate(TempFrontWindow);
- end;
- end;
- end;
-
- nullEvent:
- sysbeep(10); {should not come here}
- otherwise
-
- end; {case}
- end
- end;
-
-
- {******************** The Event Loop *****************}
-
- procedure MainEventLoop;
- const
- cmyEventLoopDelay=cEventLoopDelay; {3=1/20 second. recommended interval between WaitNextEvent
- for human interaction. we take 1 for machine responses}
- var
- newEvent: Eventrecord; { Event from getnextEvent. }
- hasWNE: boolean;
- Eventreceived: boolean;
- begin
-
- hasWNE := system.WNEIsImplemented;
- gWaitNextEventSleep := 0; {kick off}
- gNextNetRefresh:=tickCount;
-
- {we are in the main event loop. we do not want to
- spoil any tick. so we unroll our loop}
- if hasWNE then
- repeat { Loop until done. }
- Eventreceived := WaitNextEvent(everyEvent, newEvent, gWaitNextEventSleep, nil);
- {NIL: no mouse moved is wanted}
- updatecursor; { Change the cursor shape if appropriate. }
-
- if gInBackGround then gWaitNextEventSleep:=CBackgroundSleep {set proposal for next round}
- else gWaitNextEventSleep:=CForeGroundSleep;
-
- SetEventLoopTime(cmyEventLoopDelay);
- if Eventreceived then
- DoEvent(newEvent)
- else {no real event, just timeout}
-
- {*******************************}
- repeat
-
- NetWorkScheduler.periodictask;
- if NLTask<>NoErr then ProgramBreak('NlTask Error');
- gWaitNextEventSleep:=NetWorkScheduler.GetSleep;
- MyTask(BackContinue); {newEvent}
-
- until (gTaskState<>TaskOk) |
- {IsEventLoopTime:=} (LongIntPtr(Ticks)^>=gNextEventLoopTime);{IsEventLoopTime;}
- {*******************************}
-
- if PAbortFlag then
- gTaskState := TaskCancel;
- if gTaskState in [TaskExit, TaskFatal, TaskAbort] then
- gAppDone := true;
- until gAppDone
-
- else repeat { Loop until done. }
-
- systemtask; { Let the system do its stuff. }
- updatecursor; { Change the cursor shape if appropriate. }
- Eventreceived := getnextEvent(everyEvent, newEvent);
-
- if gInBackGround then gWaitNextEventSleep:=CBackgroundSleep {set proposal for next round}
- else gWaitNextEventSleep:=CForeGroundSleep;
-
- SetEventLoopTime(cmyEventLoopDelay);
- if Eventreceived then
- DoEvent(newEvent)
- else {no real event, just timeout}
-
- {*******************************}
- repeat
-
- NetWorkScheduler.periodictask;
- if NLTask<>NoErr then ProgramBreak('NlTask Error');
- gWaitNextEventSleep:=NetWorkScheduler.GetSleep;
- MyTask(BackContinue); {newEvent}
-
- until (gTaskState<>TaskOk) |
- {IsEventLoopTime:=} (LongIntPtr(Ticks)^>=gNextEventLoopTime);{IsEventLoopTime;}
- {*******************************}
-
- if PAbortFlag then
- gTaskState := TaskCancel;
- if gTaskState in [TaskExit, TaskFatal, TaskAbort] then
- gAppDone := true;
- until gAppDone;
-
- end;{ End of main event loop. }
-
- end.